package org.vison.wonfu.framework.commons.tools;

import org.springframework.util.StopWatch;

public class StopWatchSecondFormatter {

    public static String prettyPrintInSeconds(StopWatch stopWatch) {
        StringBuilder sb = new StringBuilder();
        // 总耗时（秒，保留2位小数）
        double totalSeconds = stopWatch.getTotalTimeSeconds();
        sb.append("StopWatch '").append(stopWatch.getId()).append("': running time = ")
                .append(String.format("%.2f", totalSeconds)).append("s\n");

        // 分隔线
        sb.append("----------------------------------------\n");
        sb.append("s      %     Task name\n");
        sb.append("----------------------------------------\n");

        // 每个任务的耗时（秒）和占比
        for (StopWatch.TaskInfo task : stopWatch.getTaskInfo()) {
            double taskSeconds = task.getTimeSeconds();
            int percent = (int) (taskSeconds / totalSeconds * 100);
            // 格式化：秒（保留2位小数）、百分比、任务名
            sb.append(String.format("%.2f  %3d%%  %s\n", taskSeconds, percent, task.getTaskName()));
        }
        return sb.toString();
    }
}
